package com.anxuan.demo.Service;


import com.anxuan.demo.mapper.PlayersRowMapper;
import com.anxuan.demo.pojo.Players;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PlayerRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Players> findAll(){
        return jdbcTemplate.query("select * from player",new PlayersRowMapper());

    }


    public Players findById(int player_id) {
        return jdbcTemplate.queryForObject("select * from player where player_id = ?",new Object[]{player_id},new PlayersRowMapper());
    }

    public Players create(Players players) {
        String sql = "insert into player(player_id,team_id,player_name,height) value(?,?,?,?)";
        jdbcTemplate.update(sql,new Object[]{players.getPlay_id(),players.getTeam_id(),players.getPlayer_name(),players.getHeight()});
        return players;
    }

    public void delete(int player_id){
        String sql = "delete from player where player_id = ?";
        jdbcTemplate.update(sql,player_id);
    }
}
